package com.dd.dc.base.exception;

import com.dd.dc.base.domain.DResult;
import com.dd.dc.common.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 描述: 全局异常处理类 @ControllerAdvice
 * 实现异常的全局拦截和统一处理功能
 * @author: yanglin
 * @Date: 2021-03-08-10:10
 * @Version: 1.0
 */
@Slf4j
@RestController
@ControllerAdvice
public class GlobalExceptionHandler {

    @Resource
    MessageSource messageSource;

    @ExceptionHandler({org.springframework.web.bind.MissingServletRequestParameterException.class})
    @ResponseBody
    public DResult processRequestParameterException(HttpServletRequest request,
                                                    HttpServletResponse response,
                                                    MissingServletRequestParameterException e) {
        response.setStatus(HttpStatus.FORBIDDEN.value());
        response.setContentType("application/json;charset=UTF-8");
        DResult result = new DResult();
        result.setCode(ResultCode.USER_UPLOAD_FILE_ERROR.getCode());
        result.setMsg(
                messageSource.getMessage(ResultCode.USER_UPLOAD_FILE_ERROR.getMsg(),
                        null, LocaleContextHolder.getLocale()) + e.getParameterName());
        return result;
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public DResult processDefaultException(HttpServletResponse response,
                                               Exception e) {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        response.setContentType("application/json;charset=UTF-8");
        DResult result = new DResult();
        result.setCode(ResultCode.USER_UPLOAD_FILE_ERROR.getCode());
        result.setMsg(messageSource.getMessage(ResultCode.USER_UPLOAD_FILE_ERROR.getMsg(), null,
                LocaleContextHolder.getLocale()));
        return result;
    }

    @ExceptionHandler(ApiException.class)
    @ResponseBody
    public DResult processApiException(HttpServletResponse response,
                                           ApiException e) {
        DResult result = new DResult();
        response.setStatus(e.getApiResultStatus().getHttpStatus());
        response.setContentType("application/json;charset=UTF-8");
        result.setCode(e.getApiResultStatus().getApiResultStatus());
        String message = messageSource.getMessage(e.getApiResultStatus().getMessageResourceName(),
                null, LocaleContextHolder.getLocale());
        result.setMsg(message);
        log.error("Knowned exception", e.getMessage(), e);
        return result;
    }

    /**
     * 内部微服务异常统一处理方法
     */
    @ExceptionHandler(InternalApiException.class)
    @ResponseBody
    public DResult processMicroServiceException(HttpServletResponse response,
                                                    InternalApiException e) {
        response.setStatus(HttpStatus.OK.value());
        response.setContentType("application/json;charset=UTF-8");
        DResult result = new DResult();
        result.setCode(e.getCode());
        result.setMsg(e.getMessage());
        return result;
    }
}
